//
//  ShellGrabOrderViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellGrabOrderViewModel.h"
@interface ShellGrabOrderViewModel()

@property (nonatomic,assign) NSInteger pageIndex;

@end
@implementation ShellGrabOrderViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        @weakify(self);
        [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPUserGrabOrderSuccessNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
            @strongify(self);
            [self getOrder:YES];
        }];
        
        //从登陆成功
       [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLoginSuccessNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
           @strongify(self);
           [self getReadAccount];
       }];
        
        
    }
    return self;
}

/**
Convert 获取全网数据
*/
- (void)getSettlData{
    
    self.dataModel = [ShellSettlDataModel new];
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:kGetSettlData params:nil success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            @strongify(self);
            self.dataModel = [ShellSettlDataModel mj_objectWithKeyValues:responseObject[kResultData]];
        }
        [LZNetworkingManager lz_request:@"get" url:kUserStatusTextDesc params:nil success:^(id  _Nullable responseObject) {
              if([responseObject[kResultStatus] isEqualToString:kCode]){
                  @strongify(self);
                  NSString *textDesc = responseObject[kResultData];
                  self.dataModel.textDesc = textDesc;
            }
        } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:NO isFailTip:NO];

        
        [self.reloadSubject sendNext:@(5)];
    } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:NO isFailTip:YES];
     
   
}


/**
Convert 获取用户状态
*/
- (void)getStatus{
   
    
    @weakify(self);
    if(k_userToken.length == 0)return;
    [LZNetworkingManager lz_request:@"get" url:kGetStatus params:nil success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            @strongify(self);
            NSString *status = responseObject[kResultData];
            status = [NSString lz_emptyStr:status];
            if([status isEqualToString:@"on"]){
                self.userStatus = YES;
            }
            [self.reloadSubject sendNext:@(4)];
        }
    } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:NO isFailTip:YES];
}

/**
Convert 修改用户状态
@param  status  on:接单 off：停止接单
*/
- (void)updateStatus:(NSString *)status{
    
    
    NSDictionary *params = @{@"status":status};
    
   @weakify(self);
   [LZNetworkingManager lz_request:@"post" url:kUpdateStatus params:params success:^(id  _Nullable responseObject) {
       if([responseObject[kResultStatus] isEqualToString:kCode]){
           @strongify(self);
           if([status isEqualToString:@"on"]){
               self.userStatus = YES;
           } else {
               self.userStatus = NO;
           }
           [self.reloadSubject sendNext:@(4)];
       }
   } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
    
    
}
/**
 Convert 获取订单
 @param  isFirst 是否是第一次
 */
- (void)getOrder:(BOOL)isFirst{
    
    if(k_userToken.length == 0){
        [self.reloadSubject sendNext:@(0)];
        return;
    };

    if (isFirst) {
        self.pageIndex = 1;
        [self.dataArray removeAllObjects];
    }

    NSDictionary *params = @{@"status":@"10"};
    NSString *url = [NSString stringWithFormat:@"%@/%ld",kQueryOrderList,(long)self.pageIndex];

    @weakify(self);
    [LZNetworkingManager  lz_request:@"get" url:url params:params success:^(id  _Nullable responseObject) {
        @strongify(self);
        dismiss(LZCurrentView);
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            NSArray *dataArray = responseObject[kResultData];
            
            for (int i = 0; i < dataArray.count; i++) {
                ShellOrderModel *orderModel = [ShellOrderModel mj_objectWithKeyValues:dataArray[i]];
                [self.dataArray addObject:orderModel];
            }
            
            //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
            NSInteger totalCount = [responseObject[@"total"] integerValue];
            if(self.dataArray.count == totalCount && self.dataArray.count > 0){
                [self.reloadSubject sendNext:@(0)];
                return;
            }
            //看是否有数据
            if(self.dataArray.count == 0){
                [self.reloadSubject sendNext:@(-1)];
            } else {
                self.pageIndex++;
                [self.reloadSubject sendNext:@(1)];
            }
        } else {
            [self.reloadSubject sendNext:@(0)];
        }
    } failure:^(NSError * _Nullable error) {
        
    } notwork:^(NSError * _Nullable error) {
        
    } isLoading:NO isFailTip:YES];
}

/**
 Convert 请求我的账户信用, 算力
 */
- (void)getReadAccount{
    
    if(k_userToken.length == 0)return;
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:kReadAccount params:nil success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            @strongify(self);
            NSDictionary *dataDictionary = responseObject[kResultData];
            self.accountModel = [ShellReadAccountModel mj_objectWithKeyValues:dataDictionary];

            [self.reloadSubject sendNext:@(6)];
        }
    } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:NO isFailTip:YES];
}

@end
